<template>
  <div class="admin-layout">
    <el-container>
      <el-aside width="280px" class="sidebar">
        <div class="logo">
          <h2>健康档案管理系统</h2>
          <p>管理员端</p>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409eff"
        >
          <el-menu-item index="/admin/elderly">
            <el-icon><User /></el-icon>
            <span>老人档案管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/reports">
            <el-icon><Document /></el-icon>
            <span>健康报告</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-container>
        <el-header class="header">
          <div class="header-left">
            <h3>{{ pageTitle }}</h3>
          </div>
          
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-info">
                <el-avatar :size="32" :src="userStore.currentUser?.avatar">
                  {{ userStore.currentUser?.name?.charAt(0) }}
                </el-avatar>
                <span class="username">{{ userStore.currentUser?.name }}</span>
                <el-icon><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <el-main class="main-content">
          <div class="content-wrapper">
            <router-view />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const activeMenu = computed(() => route.path)
const pageTitle = computed(() => {
  const pathMap: Record<string, string> = {
    '/admin/elderly': '老人档案管理',
    '/admin/reports': '健康报告'
  }
  return pathMap[route.path] || '管理后台'
})

const handleCommand = async (command: string) => {
  if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      
      userStore.logout()
      router.push('/')
    } catch {
      // 用户取消
    }
  } else if (command === 'profile') {
    // 可以跳转到个人信息页面
    console.log('个人信息')
  }
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  width: 100vw;
}

.sidebar {
  background-color: #304156;
  color: white;
  height: 100vh;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.logo {
  padding: 24px 20px;
  text-align: center;
  border-bottom: 1px solid #435266;
  background: linear-gradient(135deg, #304156 0%, #3a4a5c 100%);
}

.logo h2 {
  margin: 0 0 8px 0;
  font-size: 20px;
  color: #fff;
  font-weight: 600;
}

.logo p {
  margin: 0;
  font-size: 13px;
  color: #bfcbd9;
  background: rgba(64, 158, 255, 0.1);
  padding: 4px 12px;
  border-radius: 12px;
  display: inline-block;
}

.sidebar-menu {
  border: none;
  margin-top: 16px;
}

.sidebar-menu .el-menu-item {
  height: 56px;
  line-height: 56px;
  margin: 4px 16px;
  border-radius: 8px;
  transition: all 0.3s;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #435266 !important;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: #409eff !important;
  color: #fff !important;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 64px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-left h3 {
  margin: 0;
  color: #333;
  font-size: 20px;
  font-weight: 600;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 8px;
  transition: all 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.username {
  margin: 0 8px;
  color: #333;
  font-size: 14px;
  font-weight: 500;
}

.main-content {
  background-color: #f0f2f5;
  padding: 0;
  height: calc(100vh - 64px);
}

.content-wrapper {
  height: 100%;
  padding: 24px;
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 1400px) {
  .sidebar {
    width: 260px !important;
  }
  
  .content-wrapper {
    padding: 20px;
  }
}

@media (max-width: 1200px) {
  .sidebar {
    width: 240px !important;
  }
  
  .header {
    padding: 0 20px;
  }
  
  .content-wrapper {
    padding: 16px;
  }
}

@media (max-width: 768px) {
  .sidebar {
    width: 200px !important;
  }
  
  .header {
    padding: 0 16px;
  }
  
  .content-wrapper {
    padding: 12px;
  }
  
  .logo {
    padding: 20px 16px;
  }
  
  .logo h2 {
    font-size: 16px;
  }
}
</style> 